Account.jsx ➔ fetchData   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 7
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 3
1
import React from "react";
2
import { Profile, History, Prepaid, Balance } from "../components";
3
import profile from "../models/profile.js";
4
import auth from "../models/auth.js";
5
import { useState, useEffect } from "react";
6
import { useStateContext } from "../contexts/ContextProvider";
7
import storage from "../models/storage";
8
const Account = () => {
9
  const [userData, setUserData] = useState({});
10
  const { isGoogleAcc, isLoggedIn } = useStateContext();
11
12
  useEffect(() => {
13
    if (isGoogleAcc) {
14
      fetchDataGoogle();
15
    } else {
16
      fetchData();
17
    }
18
  }, []);
19
20
  const reedemPrepaid = async (code) => {
21
    await profile.addFund(userData._id, code, isGoogleAcc);
22
    if (isGoogleAcc) {
23
      fetchDataGoogle();
24
    } else {
25
      fetchData();
26
    }
27
  };
28
29
  async function fetchData() {
30
    console.log("-----------ACCOUNT----------");
31
    const id = storage.readJwtID();
32
    const res = await profile.getUserInformation(id, false);
33
    setUserData(res.user);
34
    console.log("----------------------------");
35
  }
36
37
  async function fetchDataGoogle() {
38
    console.log("-----------ACCOUNT----------");
39
    console.log("isGoogleAcc", isGoogleAcc);
40
    console.log("isLoggedIn", isLoggedIn);
41
    const resGoogle = await auth.getUser();
42
    console.log("resGoogle._id", resGoogle._id);
43
    console.log("isGoogleAcc", isGoogleAcc);
44
    const res = await profile.getUserInformation(resGoogle._id, isGoogleAcc);
45
    console.log("Res User data", res);
46
    setUserData(res.user);
47
    console.log("----------------------------");
48
  }
49
  return (
50
    <>
51
      <div className="w-full flex flex-col py-4 px-7">
52
        <div className="w-full py-8">
53
          <Profile userData={userData} />
54
        </div>
55
        <div className="flex flex-row">
56
          <div className="w-2/3">
57
            <History userData={userData} />
58
          </div>
59
          <div className="w-1/3 flex flex-col">
60
            <div className="mb-10">
61
              <Balance balance={userData.balance} />
62
            </div>
63
            <Prepaid reedemPrepaid={reedemPrepaid} />
64
          </div>
65
        </div>
66
      </div>
67
    </>
68
  );
69
};
70
71
export default Account;
72